package com.feicheng.controller.system;

import com.alibaba.fastjson.JSONObject;
import com.feicheng.common.result.ResultInfo;
import com.feicheng.persistence.entity.Dept;
import com.feicheng.persistence.entity.Flow;
import com.feicheng.service.impl.FOrgServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/admin/dept")
public class DeptController {

    @Autowired
    FOrgServiceImpl orgServiceImpl;
    
    
    @GetMapping("/info")
    public ResultInfo getDeptByDeptId(Integer deptId) {
    	return orgServiceImpl.getDept(deptId);
    }
    @DeleteMapping("/info")
    public ResultInfo deleteDeptById(Integer deptId) {
    	return orgServiceImpl.deleteDept(deptId);
    }
    
    @PostMapping("/info")
    public ResultInfo newDept(String dept) {
    	Dept deptObject = JSONObject.parseObject(dept,Dept.class);
    	
    	JSONObject param = new JSONObject();
    	
		param.put("dept", deptObject);
		
    	return orgServiceImpl.newDept(param);
    }
    
    @PutMapping("/info")
    public ResultInfo updateDept(String dept) {
    	Dept deptObject = JSONObject.parseObject(dept, Dept.class);
    	JSONObject param = new JSONObject();
    	param.put("dept", deptObject);
        return orgServiceImpl.updateDept(param);
    }

    
	@GetMapping("/flow/all")
	public ResultInfo getFlowByAll(Integer deptId,String name,Integer jobId) {

    	JSONObject param = new JSONObject();
    	param.put("deptId", deptId);
        param.put("name", name);
        param.put("jobId", jobId);
		return orgServiceImpl.getFlowByAll(param);
	}
	
	@GetMapping("/flow/info")
	public ResultInfo getFlow(Integer flowId) {
		
		return orgServiceImpl.getFlow(flowId);
	}
	
	@PostMapping("/flow/info")
	public ResultInfo newFlow(String flow,String steps) {
		
		Flow flowObject = JSONObject.parseObject(flow, Flow.class);
    	JSONObject param = new JSONObject();
    	param.put("flow", flowObject);
        param.put("steps", steps);
		return orgServiceImpl.newFlow(param);
	}
	
	@PutMapping("/flow/info")
	public ResultInfo updateFlow(String flow,String steps) {
		
		Flow flowObject = JSONObject.parseObject(flow, Flow.class);
    	JSONObject param = new JSONObject();
    	param.put("flow", flowObject);
        param.put("steps", steps);
		return orgServiceImpl.updateFlow(param);
	}
    
	@DeleteMapping("/flow/info")
	public ResultInfo deleteFlow(Integer flowId) {
		
		return orgServiceImpl.deleteFlow(flowId);
	}
    
  
    @GetMapping("/page")
    public ResultInfo getDeptPageLikeName(Integer current,Integer size,String name)
    {
    	JSONObject paramJsonObject = new JSONObject();
    	paramJsonObject.put("name",name);
    	return orgServiceImpl.getDeptByPage(current, size, paramJsonObject);
    }
    
    @GetMapping("/job/all")
    public ResultInfo getJobByAll(Integer deptId,Integer pJobId){
    	JSONObject param = new JSONObject();
    	param.put("deptId",deptId);
    	param.put("pJobId",pJobId);
    	return orgServiceImpl.getJobByAll(param);
    }

    @GetMapping("/job/info")
    public ResultInfo getJob(Integer jobId) {
    	return orgServiceImpl.getJob(jobId);
    }

    @PostMapping("/job/info")
    public ResultInfo newJob(Integer deptId,String name,Integer pJobId) {
    	JSONObject param = new JSONObject();
    	param.put("deptId",deptId);
    	param.put("name",name);
    	param.put("pJobId",pJobId);
    	return orgServiceImpl.newJob(param);
    }

    @PutMapping("/job/info")
    public ResultInfo updateJob(Integer jobId,Integer deptId,String name,Integer pJobId) {
    	JSONObject param = new JSONObject();
    	param.put("jobId", jobId);
    	param.put("deptId",deptId);
    	param.put("name",name);
    	param.put("pJobId",pJobId);
    	return orgServiceImpl.updateJob(param);
    }


    @DeleteMapping("/job/info")
    public ResultInfo deleteJob(Integer jobId) {
    	
    	return orgServiceImpl.deleteJob(jobId);
    }
    
    //部门全查询
    @GetMapping("/all")
    public ResultInfo getDeptAll(String name) {
    	
    	JSONObject param = new JSONObject();
    	param.put("name", name);
    	
    	return orgServiceImpl.getDeptByAll(param);
    }
}
